﻿using Furion.DatabaseAccessor;
using Furion.DynamicApiController;
using System.Security.Claims;

/// <summary>
/// 基类
/// </summary>
public partial class BaseServices : IDynamicApiController
{
    /// <summary>
    /// 角色
    /// </summary>
    public Enums.TokenRole ROLE
    {
        get
        {
            int role = App.User?.FindFirstValue("Role").ObjToInt() ?? 0;

            return (Enums.TokenRole)role;
        }
    }

    /// <summary>
    /// 当前登录的用户
    /// </summary>
    public int USERID
    {
        get
        {
            return App.User?.FindFirstValue("UserId").ObjToInt() ?? 0;
        }
    }

    /// <summary>
    /// 当前登录的商户
    /// </summary>
    public int STOREID
    {
        get
        {
            return App.User?.FindFirstValue("StoreId").ObjToInt() ?? 0;
        }
    }

    /// <summary>
    /// 登录的代理 
    /// </summary>
    public int PROXYID
    {
        get
        {
            return App.User?.FindFirstValue("ProxyId").ObjToInt() ?? 0;
        }
    }

    
   

    /// <summary>
    /// 根据角色自动获取ID
    /// </summary>
    public int AUTOID
    {
        get
        {
            if (ROLE == Enums.TokenRole.代理)
            {
                return PROXYID;
            }

            if (ROLE == Enums.TokenRole.客户)
            {
                return USERID;
            }

            if (ROLE == Enums.TokenRole.商家)
            {
                return STOREID;
            }

            return 0;
        }
    }

    

    /// <summary>
    /// 分页
    /// </summary>
    protected int PageNo
    {
        get
        {
            try
            {
                int pageno = App.HttpContext.Request.Headers["pageno"].ObjToInt();

                return pageno == 0 ? 1 : pageno;
            }
            catch (Exception)
            {
                return 1;
            }
        }
    }

    /// <summary>
    /// 分页大小
    /// </summary>
    protected int PageSize
    {
        get
        {
            try
            {
                int pagesize = App.HttpContext.Request.Headers["pagesize"].ObjToInt();

                return pagesize == 0 ? 20 : pagesize;
            }
            catch (Exception)
            {
                return 20;
            }
        }
    }

    private object Json(bool state, string msg, object data, int code = 200)
    {
        return new { state, msg, data, code };
    }

    /// <summary>
    /// 成功响应
    /// </summary>
    /// <param name="data"></param>
    /// <param name="code"></param>
    /// <returns></returns>
    protected object Ok(object data, int code = 200)
    {
        return Json(true, "", data, code);
    }

    /// <summary>
    /// 失败响应
    /// </summary>
    /// <param name="msg"></param>
    /// <param name="code"></param>
    /// <returns></returns>
    protected object No(string msg, int code = 200)
    {
        return Json(false, msg, new { }, code);
    }

    /// <summary>
    /// 通用响应
    /// </summary>
    /// <param name="state"></param>
    /// <param name="success"></param>
    /// <param name="error"></param>
    /// <returns></returns>
    protected object Msg(bool state, string success = "操作成功", string error = "操作失败")
    {
        return state ? Json(true, success, new { }) : Json(true, error, new { });
    }

}

/// <summary>
/// 基类
/// </summary>
/// <typeparam name="T"></typeparam>
public partial class BaseServices<T> : BaseServices where T : Entity.BaseEntity, new()
{
    private readonly ISqlSugarClient db;


    /// <summary>
    /// 
    /// </summary>
    /// <param name="db"></param>
    public BaseServices(ISqlSugarClient db)
    {
        this.db = db;
    }


    /// <summary>
    /// 分页获取
    /// </summary>
    /// <returns></returns>
    public virtual async Task<object> GetPage()
    {
        string search = SearchStr;

        var data = await db.Queryable<T>()

            .WhereIF(search.Length > 0, search)

            .OrderByDescending(x => x.Id)

            .ToPagedListAsync(PageNo, PageSize);

        return Ok(data);

    }

    /// <summary>
    /// 编辑数据
    /// </summary>
    /// <param name="t"></param>
    /// <returns></returns>
    public virtual async Task<object> Edit(T t)
    {
        bool state = false;

        if (t.Id == 0)
        {
            state = await db.Insertable(t).ExecuteCommandAsync() > 0;
        }
        else
        {
            state = await db.Updateable(t).ExecuteCommandHasChangeAsync();
        }

        return Msg(state);
    }

    /// <summary>
    /// 根据编号获取单条
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public virtual async Task<object> GetOne(int id)
    {
        return Ok(await db.Queryable<T>().Where(x => x.Id == id).FirstAsync());
    }

    /// <summary>
    /// 删除数据
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    protected virtual async Task<object> Remove(int id)
    {
        bool state = await db.Deleteable<T>().Where(x => x.Id == id).ExecuteCommandHasChangeAsync();

        return Msg(state);
    }

}


